package org.shop.controller.app.live;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.QuestionAnswer;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.QuestionAnswerService;
import org.shop.service.QuestionAnswerService;
import org.shop.util.CommonUtil;
import org.shop.util.ObjUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class QuestionAnswerContrller {
	private static Logger logger = LogManager.getLogger(QuestionAnswerContrller.class);
	
	@Autowired
	private QuestionAnswerService questionAnswerService;
	
    /**
     * app问答留言回复
     * @param req
     * @return
     */
    @RequestMapping("app/addQuestionReply")
    @ResponseBody
    public Map<String, Object> addQuestionReply(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        try{
        	String questionId = req.getParameter("questionId");
        	String receiverId = req.getParameter("receiverId");
        	String receiverName = req.getParameter("receiverName");
        	String receiverHeadImg = req.getParameter("receiverHeadImg");
            String content = req.getParameter("content");
            String textBlackList=CommonUtil.getPropertiesParam("textBlackList");
            for(String textBlack:textBlackList.split(",")){
                if(content.contains(textBlack)){
                    map.put("state", 2);
                    map.put("msg", "发布内容不合法");
                    return map;
                }
            }
            MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            QuestionAnswer QuestionAnswer = new QuestionAnswer();
            QuestionAnswer.setQuestionId(Long.parseLong(questionId));;
            QuestionAnswer.setContent(content);
            QuestionAnswer.setSenderId(ud.getUserId());
            QuestionAnswer.setSendName(ud.getUserName());
            QuestionAnswer.setSendHeadImg(ud.getUserPhoto());
            if(ObjUtil.isNotNullAndEmpty(receiverId)) {
            	QuestionAnswer.setReceiverId(Long.parseLong(receiverId));
            	QuestionAnswer.setReceiverName(receiverName);
            	QuestionAnswer.setReceiverHeadImg(receiverHeadImg);
            }
            questionAnswerService.insertSelective(QuestionAnswer);
            map.put("state", 1);
            map.put("msg", "发布成功");
        }catch(Exception e){
            e.printStackTrace();
            logger.error(e);
            map.put("state", 0);
            map.put("msg", "发布失败");
        }
        return map;
    }
//    
	  /**
	 * app根据id获取评论列表
	 * @param req
	 * @return
	 */
     @RequestMapping("app/getQuestionAnswerList")
     @ResponseBody
     public List<Map<String, Object>> getQuestionAnswerList(HttpServletRequest req){
	   	SecurityContextImpl securityContextImpl = (SecurityContextImpl)req.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
        MyUserDetail ud = null;
        if(ObjUtil.isNotNullAndEmpty(securityContextImpl)) {
   	      ud = (MyUserDetail) securityContextImpl.getAuthentication().getPrincipal();
        }
	    String questionId = req.getParameter("questionId");
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
		Map<String, Object> params = new HashMap<String, Object>();
		List<Map<String, Object>> questionAnswerList = null;
		try {
			params.put("questionId", questionId);
			if(ObjUtil.isNotNullAndEmpty(ud)) {
				params.put("userId", ud.getUserId());
			}
			params.put("currentPage", (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize));
			params.put("pageSize", pageSize);
			questionAnswerList = questionAnswerService.getQuestionAnswerList(params);
		}catch(Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		//   	List<Map<String, Object>> postAnswerList2 = new ArrayList<Map<String,Object>>();
		//   	for(int i=0;i<postAnswerList.size();i++) {
		//   		Map<String, Object> map = postAnswerList.get(i);
		//   		Map<String, Object> m = new HashMap<String, Object>();
		//   		m.put("targetId", map.get("answerId"));
		//   		map.put("praiseCount", praiseService.getPraiseCount(m));
		//   		postAnswerList2.add(map);
		//   	}
	       return questionAnswerList;
	   }
    
    /**
     * 删除评论
     * @param req
     * @return
     */
    @RequestMapping("app/delQuestionAnswer")
    @ResponseBody
    public Map<String, Object> delAnswer(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        String questionAnswerId = req.getParameter("questionAnswerId");
        try {
        	int num = questionAnswerService.deleteByPrimaryKey(Long.parseLong(questionAnswerId));
            map.put("state", num);
            map.put("msg", "删除成功");
        } catch(Exception e) {
            e.printStackTrace();
            logger.error(e);
            map.put("state", 0);
            map.put("msg", "删除失败");
        }
        return map;
    }
}
